package com.longcai.controller.homegood;


import com.alibaba.fastjson.JSONObject;
import com.longcai.bean.ApiResult;
import com.longcai.entity.homegood.ClyjGoodsHomepage;
import com.longcai.service.homegood.ClyjGoodsHomepageService;
import com.longcai.util.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 首页商品表 前端控制器
 * </p>
 *
 * @author LWK
 * @since 2019-03-22
 */
@Controller
@RequestMapping("/clyjGoodsHomepage")
public class ClyjGoodsHomepageController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjGoodsHomepageService clyjGoodsHomepageService;

    /**
     * 商品存入solr
     *
     * @return
     */
    @RequestMapping("/saveGoodSolr")
    @ResponseBody
    public ApiResult saveGoodSolr() {

        return this.clyjGoodsHomepageService.saveGoodSolr();
    }

    /**
     * 通过类型查询商品
     *
     * @param params
     * @return
     */
    @RequestMapping("/getGoodsBytype")
    @ResponseBody
    public ApiResult getGoodsBytype(@RequestBody JSONObject params) {
        String type = params.getString("type");
        return this.clyjGoodsHomepageService.getGoodsBytype(type);

    }

    /**
     * 通过品牌从solr查询商品
     *
     * @param params
     * @return
     */
    @RequestMapping("/getGoodsBybrand")
    @ResponseBody
    public Page<ClyjGoodsHomepage> getGoodsBybrand(@RequestBody JSONObject params) {
        String brand = params.getString("brand");
        return this.clyjGoodsHomepageService.getGoodsBybrand(brand);

    }

    /**
     * 查询商品详情
     *
     * @param params
     * @return
     */
    @RequestMapping("/getGoods")
    @ResponseBody
    public Map<String, Object> getGoods(@RequestBody JSONObject params) {
        String goodsid = params.getString("goodsid");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            if (StringUtils.isNotEmpty(goodsid) && !goodsid.equals("")) {
                resultMap = clyjGoodsHomepageService.getGoods(goodsid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.debug("返回参数+++++++++", resultMap);
        return resultMap;
    }

    /**
     * 查询商品规格详情
     *
     * @param params
     * @return
     */
    @RequestMapping("/getSpec")
    @ResponseBody
    public Map<String, Object> getSpec(@RequestBody JSONObject params) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            resultMap = clyjGoodsHomepageService.getSpec(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.debug("返回参数+++++++++", resultMap);
        return resultMap;
    }

    /**
     * 分享商品（生成分享二维码）
     *
     * @throws UnsupportedEncodingException
     * @throws UnsupportedEncodingException
     **/
    @RequestMapping("/shareGoodsQRcode")
    @ResponseBody
    public ApiResult shareGoodsQRcode(@RequestBody JSONObject params) throws UnsupportedEncodingException {
        return clyjGoodsHomepageService.shareGoodsQRcode(params);
    }
}

